package com.ceba.test.modules.admin.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ceba.base.enums.SealTypeEnum;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.enums.StampTypeEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.ceba.base.web.entity.IBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GStamp extends IBase{

	private static Logger logger = LoggerFactory.getLogger(GStamp.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id;
	private String userId;
	private String relactionId;
	private String fileId;
	private String matchName = "";
	private String imgWidth = "75";
	private String imgHeight = "75";
	private String filePath = CebaPathUtil.getVisualtransPic();
	private String classString;
	private String position = "";
	
	
	// 盖章类型  0-关键字 1--拖拽定位
	private String  sealType = SealTypeEnum.KEYWORD_STAMP_FLAG.getType();
	//盖章页码的方式  0--当前页   1--全部页  2--输入页码
	private String  sealPageMode="0"; 
	
	//0-用户章  1--报告章 2-一般资质章 3--骑缝  4--水印  5--草稿水印  6--水印资质章
	// 对于CorFlagEnum
	private String corFlag = "0";
	private String corStampName="";
	private String stamp_x="0";
	private String stamp_y="0";
	private String stampPage = "1";
	//信安 --专用添加的证书ID
	private String cerNo="";
	//秘钥名称
	private String keyName;
	//加密方式 1-RSA 2-SM2
	private int encodeSignType;
  	
	private boolean stampFlag = true;
	
	//用于印章权限控制
	private boolean isSelect = false;
	
	
	//以下字段非数据库中字段---------------
	private float positionRate;//骑缝章的位置比例   靠右边
	private String qfRuleId="";//骑缝章规则id
	//-------临时使用字段-------插入图片文件路径
	private String insertImagePath;
	
	private String name;

	//文件存储地址ID
	private String storageAddressId;

	
	public GStamp(String guid, String userId, String userStampName, String matchName, String imgWidth, String imgHeight,
			String stampPage, String fileName, String colFlag,String sealType) {
		this.id = guid;
		this.userId = userId;
		this.corStampName = userStampName;
		this.matchName = matchName;
		this.imgWidth = imgWidth;
		this.imgHeight = imgHeight;
		this.stampPage = stampPage;
		this.filePath = fileName;
		this.corFlag = colFlag;
		this.sealType = sealType;
	}
	
	public GStamp(String guid, String userStampName, String matchName, String imgWidth, String imgHeight,
			String stampPage, String fileName, String colFlag,String stamp_x, String stamp_y,String sealType) {
		this.id = guid;
		this.corStampName = userStampName;
		this.matchName = matchName;
		this.imgWidth = imgWidth;
		this.imgHeight = imgHeight;
		this.stampPage = stampPage;
		this.filePath = fileName;
		this.corFlag = colFlag;
		this.stamp_x = stamp_x;
		this.stamp_y = stamp_y;
		this.sealType = sealType;
	}
	public GStamp() {
	}
	public String getStampPage() {
		return stampPage;
	}
	public Integer getIntStampPage() {
		if(StringUtil.isEmptyString(stampPage)){
			stampPage = "1";
		}
		return NumberUtils.toInt(stampPage);
	}
	public void setStampPage(String stampPage) {
		this.stampPage = stampPage;
	}
	private String userHtml;
	
	public String getUserHtml() {
		return userHtml;
	}
	public void setUserHtml(String userHtml) {
		this.userHtml = userHtml;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getRelactionId() {
		return relactionId;
	}
	public void setRelactionId(String relactionId) {
		this.relactionId = relactionId;
	}
	public String getFileId() {
		return fileId;
	}
	public void setFileId(String fileId) {
		this.fileId = fileId;
	}
	public String getMatchName() {
		return matchName;
	}
	public void setMatchName(String matchName) {
		this.matchName = matchName;
	}
	public String getImgWidth() {
		return imgWidth;
	}
	public Float getFloatImgWidth() {
		return Float.parseFloat(imgWidth);
	}
	public void setImgWidth(String imgWidth) {
		this.imgWidth = imgWidth;
	}
	public String getImgHeight() {
		return imgHeight;
	}
	public Float getFloatImgHeight() {
		return Float.parseFloat(imgHeight);
	}
	public void setImgHeight(String imgHeight) {
		this.imgHeight = imgHeight;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public String getClassString() {
		return classString;
	}
	public void setClassString(String classString) {
		this.classString = classString;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public String getUpdateUser() {
		return updateUser;
	}
	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}
	public String getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}
	public String getCorFlag() {
		return corFlag;
	}
	public void setCorFlag(String corFlag) {
		this.corFlag = corFlag;
	}
	public String getCorStampName() {
		return corStampName;
	}
	public void setCorStampName(String corStampName) {
		this.corStampName = corStampName;
	}
	public String getStamp_x() {
		return stamp_x;
	}
	public Float getFloatStamp_x() {
		return Float.parseFloat(stamp_x);
	}
	public void setStamp_x(String stamp_x) {
		this.stamp_x = stamp_x;
	}
	public String getStamp_y() {
		return stamp_y;
	}
	public Float getFloatStamp_y() {
		return Float.parseFloat(stamp_y);
	}
	public void setStamp_y(String stamp_y) {
		this.stamp_y = stamp_y;
	}
	
	public boolean isStampFlag() {
		return stampFlag;
	}
	public void setStampFlag(boolean stampFlag) {
		this.stampFlag = stampFlag;
	}
	public String getInsertImagePath() {
		return insertImagePath;
	}
	public void setInsertImagePath(String insertImagePath) {
		this.insertImagePath = insertImagePath;
	}
	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	public String getCerNo() {
		return cerNo;
	}

	public void setCerNo(String cerNo) {
		this.cerNo = cerNo;
	}

	public int getEncodeSignType() {
		return encodeSignType;
	}

	public void setEncodeSignType(int encodeSignType) {
		this.encodeSignType = encodeSignType;
	}
	
	public float getPositionRate() {
		return positionRate;
	}

	public void setPositionRate(float positionRate) {
		this.positionRate = positionRate;
	}

	public String getSealType() {
		return sealType;
	}

	public void setSealType(String sealType) {
		this.sealType = sealType;
	}
	
	

	public String getSealPageMode() {
		return sealPageMode;
	}

	public void setSealPageMode(String sealPageMode) {
		this.sealPageMode = sealPageMode;
	}

	
	public String getQfRuleId() {
		return qfRuleId;
	}

	public void setQfRuleId(String qfRuleId) {
		this.qfRuleId = qfRuleId;
	}
	
	

	public boolean isSelect() {
		return isSelect;
	}

	public void setSelect(boolean isSelect) {
		this.isSelect = isSelect;
	}

	public String getStorageAddressId() {
		return storageAddressId;
	}

	public void setStorageAddressId(String storageAddressId) {
		this.storageAddressId = storageAddressId;
	}

	public String getKeyName() {
		return keyName;
	}

	public void setKeyName(String keyName) {
		this.keyName = keyName;
	}

	/**
	 * 对象封装成iSign
	 * @param gs
	 * @return
	 */
	public static ISign packToI_Signs(GStamp gs,ISign iSign) {
		iSign.setStampFlag(gs.isStampFlag()?"true":"false");
		iSign.setStampPath(gs.getFilePath());
		iSign.setImgWidth(gs.getFloatImgWidth()+"");
		iSign.setImgHeight(gs.getFloatImgHeight()+"");
		iSign.setStampX(gs.getFloatStamp_x()+"");
		iSign.setStampY(gs.getFloatStamp_y()+"");
		iSign.setSignType(Integer.valueOf(gs.getSealType()));
		iSign.setStampPage(gs.getIntStampPage()+"");
		iSign.setStampName(gs.getCorStampName());
		iSign.setMatchName(gs.getMatchName());
		iSign.setPrivateKeyName(gs.getKeyName());
		logger.info("packToI_Signs: privateKeyName=" + iSign.getPrivateKeyName());
		return iSign;
	}
	
	
	/**
	 * CorStampName 字符串处理
	 * @return
	 */
	public String getCorStampNameStr(){
		String result=null;
		if(StringUtils.isNotBlank(corStampName) &&  corStampName.length()>4){
			result=corStampName.substring(0, 4)+"...";
		}
		return result;
	}
	/*
	 * 解析从前台字典数据封装成对象
	 */
	public static GStamp parseFromFront(Map<String, String> dic) {
		GStamp gs = new GStamp();
		
		if(dic.containsKey("matchName")){
			gs.setMatchName(dic.get("matchName"));
		}
		if(dic.containsKey("ID")){
			gs.setId(dic.get("ID"));
		}
		if(dic.containsKey("stampName")){
			gs.setCorStampName(dic.get("stampName"));
		}
		if(dic.containsKey("imgWidth")){
			gs.setImgWidth(dic.get("imgWidth"));
		}
		if(dic.containsKey("imgHeight")){
			gs.setImgHeight(dic.get("imgHeight"));
		}
		if(dic.containsKey("stampPage")){
			gs.setStampPage(dic.get("stampPage"));
		}
		if(dic.containsKey("stampX")){
			gs.setStamp_x(dic.get("stampX"));
		}
		if(dic.containsKey("stampY")){
			gs.setStamp_y(dic.get("stampX"));
		}
		
		if(dic.containsKey("cerNo")){
			gs.setCerNo(dic.get("cerNo"));
		}
		if(dic.containsKey("encodeSignType")){
			gs.setEncodeSignType(Integer.valueOf(dic.get("ENCODE_SIGNTYPE")));
		}
		return gs;
	}

	//克隆属性
	public static void parseFrontOtherStamp(GStamp gsFront,GStamp gs){
		gs.setId(gsFront.getId());
		gs.setImgWidth(gsFront.getImgWidth());
		gs.setImgHeight(gsFront.getImgHeight());
		gs.setMatchName(gsFront.getMatchName());
		gs.setFilePath(gsFront.getFilePath());
		gs.setStamp_x(gsFront.getStamp_x());
		gs.setStamp_y(gsFront.getStamp_y());
		gs.setPosition(gsFront.getPosition());
		gs.setStampPage(gsFront.getStampPage());
		gs.setCorFlag(gsFront.getCorFlag());
		gs.setCorStampName(gsFront.getCorStampName());
		gs.setUserId(gsFront.getUserId());
		gs.setSealPageMode(gsFront.getSealPageMode());
		gs.setSealType(gs.getSealType());
	}
	
	
	//将前台传递的stamp JSONLIST转map
	public static void packageJsonToList(JSONArray arrList,Map<String,List<GStamp>> gsMaps){
		if(arrList != null && arrList.length() >0){
			for(int i = 0 ; i < arrList.length() ; i++){
				JSONObject o = arrList.getJSONObject(i);
				GStamp gs = new GStamp();
				if(o.has("matchName")){
					gs.setMatchName(o.getString("matchName"));
				}
				if(o.has("corStampName")){
					gs.setCorStampName(o.getString("corStampName"));
				}
				if(o.has("imgWidth")){
					gs.setImgWidth(o.getString("imgWidth"));
				}
				if(o.has("imgHeight")){
					gs.setImgHeight(o.getString("imgHeight"));
				}
				if(o.has("filePath")){
					gs.setFilePath(o.getString("filePath"));
				}
				if(o.has("corFlag")){
					gs.setCorFlag(o.getString("corFlag"));
				}
				if(o.has("stampPage")){
					gs.setStampPage(o.getString(""));
				}
				if(o.has("stamp_x")){
					gs.setStamp_x(o.getString("stamp_x"));
				}
				if(o.has("stamp_y")){
					gs.setStamp_y(o.getString("stamp_y"));
				}
				if(o.has("stampFlag")){
					gs.setStampFlag("true".equals(o.getString("stampFlag")));
				}else{
					gs.setStampFlag(true);
				}
				//用户章
				if(StampTypeEnum.USER_COR_STAMP_FLAG.getType().equals(gs.getCorFlag())){
					if(gsMaps.containsKey(StampTypeEnum.USER_COR_STAMP_FLAG.getType())){
						gsMaps.get(StampTypeEnum.USER_COR_STAMP_FLAG.getType()).add(gs);
					}else{
						List<GStamp> uGs = new ArrayList<GStamp>();
						uGs.add(gs);
						gsMaps.put(StampTypeEnum.USER_COR_STAMP_FLAG.getType(), uGs);
					}
				}
				//报告章
				if(StampTypeEnum.ENTERPRISE_COR_STAMP_FLAG.getType().equals(gs.getCorFlag())){
					if(gsMaps.containsKey(StampTypeEnum.ENTERPRISE_COR_STAMP_FLAG.getType())){
						gsMaps.get(StampTypeEnum.ENTERPRISE_COR_STAMP_FLAG.getType()).add(gs);
					}else{
						List<GStamp> eGs = new ArrayList<GStamp>();
						eGs.add(gs);
						gsMaps.put(StampTypeEnum.ENTERPRISE_COR_STAMP_FLAG.getType(), eGs);
					}
				}
			
				//骑缝章
				if(StampTypeEnum.QIFENG_COR_STAMP_FLAG.getType().equals(gs.getCorFlag())){
					if(gsMaps.containsKey(StampTypeEnum.QIFENG_COR_STAMP_FLAG.getType())){
						gsMaps.get(StampTypeEnum.QIFENG_COR_STAMP_FLAG.getType()).add(gs);
					}else{
						List<GStamp> qGs = new ArrayList<GStamp>();
						qGs.add(gs);
						gsMaps.put(StampTypeEnum.QIFENG_COR_STAMP_FLAG.getType(), qGs);
					}
				}
				//水印
				if(StampTypeEnum.WATERMARK_COR_STAMP_FLAG.getType().equals(gs.getCorFlag())){
					if(gsMaps.containsKey(StampTypeEnum.WATERMARK_COR_STAMP_FLAG.getType())){
						gsMaps.get(StampTypeEnum.WATERMARK_COR_STAMP_FLAG.getType()).add(gs);
					}else{
						List<GStamp> wGs = new ArrayList<GStamp>();
						wGs.add(gs);
						gsMaps.put(StampTypeEnum.WATERMARK_COR_STAMP_FLAG.getType(), wGs);
					}
				}
			}
		}
	}
	
	
}
